﻿using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using jSLApp.Client.ApplicationBase;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.BaseControl;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;
using jSLCore.Client.Utility;

namespace jSLApp.Client.Forms
{
    public partial class ArticleDetail : DataControl
    {
        private readonly int _articleId;

        public ArticleDetail(int articleId):this()
        {
            _articleId = articleId;
        }

        /// <summary>
        /// for designer
        /// </summary>
        public ArticleDetail()
        {
            InitializeComponent();
        }

        public override void LoadInitialData()
        {
            DoWork("Загрузка статьи",(sender, args) =>
                                         {
                                             args.Result = Communication.Proxy.GetArticle(args.Int);
                                         },(o, eventArgs) =>
                                                               {
                                                                   if(eventArgs.Error==null)
                                                                   {
                                                                       SetDataContext((New) eventArgs.Result);
                                                                   }
                                                               },_articleId);
        }

        public void SetDataContext(New ent)
        {
            Article = ent;
            var uri = new Uri(Path.Combine(UIHelper.GetCurrentWebHostAddress(), ent.BodyURL));
            browser.SetSource(uri);
            commentList.CommetableId = ent.id;
            subs.CommentableId = ent.id;
        }

        public New Article
        {
            get { return (New) DataContext; }
            set { DataContext = value; }
        }

        private void updateData(object sender, EventArgs e)
        {
            LoadInitialData();
        }

        private void copyLink_Clicked(object sender, RoutedEventArgs e)
        {
            var hlB = sender as HyperlinkButton;
            Clipboard.SetText(hlB.Content.ToString());
        }

        private void deleteIt(object sender, RoutedEventArgs e)
        {
            var dlg = new QuestionBox("Статья будет полностью удалена и восстановить ее будет нельзя. Продолжить?",QuestionBoxType.OkCancel);
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   DoWork("Удаление статьи",(sender1, args) => Communication.Proxy.DeleteArticle(args.Int),(o1, eventArgs) =>
                                                                                       {
                                                                                           if(eventArgs.Error==null)
                                                                                           {
                                                                                               InfoBox.ShowDialog("Статья успешно удалена");
                                                                                               MainContentContainer.Current.GotoArticles();
                                                                                           }
                                                                                       },Article.id);
                               },this );
        }
    }
}
